package cn.jsj.invite.controller;

import cn.jsj.invite.interceptor.swich.PassToken;
import cn.jsj.invite.pojo.InvitationInfo;
import cn.jsj.invite.pojo.StudentEnt;
import cn.jsj.invite.result.Result;
import cn.jsj.invite.service.InvitationService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@CrossOrigin
public class InvitationController {

    private final InvitationService invitationService;

    public InvitationController(InvitationService invitationService) {
        this.invitationService = invitationService;
    }

    //查看某方向的所有帖子
    @PassToken
    @GetMapping("/findAllPage/{areaId}")
    public Result<PageInfo<InvitationInfo>> findAllPage(@PathVariable Integer areaId, Integer pageNumber) {
        PageInfo<InvitationInfo> invitationInfos = invitationService.listInvitationByArea(areaId, pageNumber);
        return Result.success(invitationInfos);
    }

    //根据标签和方向查询帖子
    @PassToken
    @GetMapping("/findPageByTag")
    public Result<PageInfo<InvitationInfo>> findPageByTag(@RequestParam String tag, @RequestParam Integer area, Integer pageNumber) {
        PageInfo<InvitationInfo> invitationInfos = invitationService.listInvitationByTagAndArea(tag, area, pageNumber);
        return Result.success(invitationInfos);
    }

    //根据ID查看某个帖子的信息
    @PassToken
    @GetMapping("/findPageById/{id}")
    public Result<InvitationInfo> findPageById(@PathVariable Integer id) {
        InvitationInfo oneInvitationById = invitationService.getOneInvitationById(id);
        return Result.success(oneInvitationById);
    }

    //查看帖子发布者的联系方式
    @PostMapping("/student/contactInfo")
    public Result<?> studentContactInfo(Integer studentId, Integer invitationId) {
        StudentEnt contactInfo = invitationService.getInvitationPublisherContactInfo(studentId, invitationId);
        if (contactInfo != null) {
            return Result.success(contactInfo);
        }
        return Result.fail("参数无效");
    }

    //学生发布帖子
    @PostMapping("/student/publishPage")
    public Result<?> studentPublishPage(InvitationInfo info, @RequestParam("photo[]") MultipartFile[] photo) {
        Integer integer = invitationService.publishOneStudentInvitation(info, photo);
        if (integer == null || integer.equals(0)) {
            return Result.fail("没有该学生的信息");
        }
        if (integer.equals(1)) {
            return Result.success();
        }
        return Result.fail("添加失败");

    }


}
